package com.fhz.day19;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Toast;

import com.fhz.day19.adapter.IndexAdapter;
import com.fhz.day19.fragment.LocalFragment;
import com.fhz.day19.fragment.LoveFragment;
import com.fhz.day19.fragment.OnlineFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者 : ni
 * 日期: date
 * 功能: 启动的页面
 */
public class MainActivity extends AppCompatActivity {
    private TabLayout tab;
    private ViewPager vp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tab = (TabLayout) findViewById(R.id.tab);
        vp = (ViewPager) findViewById(R.id.vp);

        requestPermissions(new String[]{
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.INTERNET,
                Manifest.permission.READ_SMS,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.WRITE_CONTACTS,
                Manifest.permission.CALL_PHONE
        },100);
//周杰伦

        //用网的判断
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        if(connectivityManager != null){
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if(activeNetworkInfo != null && activeNetworkInfo.isAvailable()){

                Toast.makeText(MainActivity.this,"neng yong",Toast.LENGTH_LONG).show();
                //存入到数据库
            }else{
                Toast.makeText(MainActivity.this, "meiwang", Toast.LENGTH_SHORT).show();
                //从数据库里面读
                //从sp里面读
            }
        }

        List<Fragment> list = new ArrayList<>();
        list.add(new OnlineFragment());
        list.add(new LocalFragment());
        list.add(new LoveFragment());

        List<String> titles = new ArrayList<>();
        titles.add("在线歌曲");
        titles.add("本地歌曲");
        titles.add("收藏歌曲");

        IndexAdapter indexAdapter = new IndexAdapter(getSupportFragmentManager(),list,titles);

        vp.setAdapter(indexAdapter);

        tab.setupWithViewPager(vp);

        vp.setOffscreenPageLimit(2);

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            //同意 邓紫棋
        }{
            //不同意啥也不干
        }
    }
}